<template>
    <view>
        <custom>
            <template #backText>{{page.form.address_id ? '编辑' : '添加'}}地址</template>
            <template #right v-if="page.form.address_id">
                <view class="action" @tap="deleteDialog = true">
                    <text class="cuIcon-delete"></text>
                </view>
            </template>
        </custom>
        <nFormItem title="联系人" :data.sync="data" field="name"></nFormItem>
        <nFormItem type="radio_is" title="" :data.sync="data" field="sex" :radio_values="['先生', '女士']"></nFormItem>
        <nFormItem title="手机号" :data.sync="data" field="phone"></nFormItem>
        <formProvinceCityArea title="所在地区" :data.sync="data"></formProvinceCityArea>
        <nFormItem title="详细地址" placeholder="例: xx小区x号楼x单元xxxx室" :data.sync="data" field="address_remark"></nFormItem>
        <nFormItem type="radio_tag_optional" title="标签" :data.sync="data" field="tag" :radio_values="{1: '家', 2: '公司'}"
            radio_default_value="0"></nFormItem>
        <nFormItem type="switch" title="设为默认地址" title2="下单时默认选择的地址" :data.sync="data" field="is_default"></nFormItem>
        <nFormButton title="保存" :tap="update"></nFormButton>
        <modalDialog :show.sync="deleteDialog" :confirm="update" content="确定要删除该地址吗？">
        </modalDialog>
    </view>
</template>

<script>
    import formProvinceCityArea from '@/components/nddyny/form/provinceCityArea.vue';
    export default {
        components: {
            formProvinceCityArea,
        },
        data() {
            return {
                deleteDialog: false,
                page: {
                    form: {
                        address_id: 0
                    }
                },
                data: {
                    formtag: {},
                    form: {
                        name: '',
                        sex: '',
                        phone: '',
                        area_code: 0,
                        address_remark: '',
                        tag: 0,
                        is_default: 0,
                        is_delete: 0
                    }
                }
            }
        },
        onLoad(params) {
            if (!this.$nddyny.object.empty(params)) {
                this.page.form = params;
            }
        },
        methods: {
            update() {
                const is_delete = this.deleteDialog ? 1 : 0;
                const message_type = is_delete == 0 ? '保存' : '删除';
                this.$api.post(this, {
                    loading: message_type + '中',
                    url: '/user/address/update',
                    data_append: {
                        is_delete: is_delete
                    },
                    success: (Result) => {
                        if (this.$api.form.toast(this, Result)) return;
                        if (is_delete) this.deleteDialog = false;
                        this.$store.commit('userToastSuccess', {
                            message: message_type + '成功',
                            commit: 'appBackPage'
                        });
                    }
                });
            }
        },
        watch: {
            'page.form'() {
                this.$api.post(this, {
                    url: '/user/address/info',
                    data_key: 'page',
                    success: (Result, data_key) => {
                        if (this.$api.form.toast(this, Result, data_key)) return;
                        this.data.form = Result.result;
                    }
                });
            }
        }
    }
</script>
